package org.lee.simg;

import javax.swing.*;
import java.awt.*;

/**
 * DisplayImage
 *
 * @author dralee
 * @date 2021-12-28 11:29:15
 **/
public class DisplayImage extends JFrame {
    public DisplayImage() throws HeadlessException {
        initUI();
    }

    private void initUI() {
        ImageIcon icon = loadImage();
        JLabel lbl = new JLabel(icon);
        createLayout(lbl);

        setTitle("Image");
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    private void createLayout(JComponent ...arg) {
        Container pane = getContentPane();
        GroupLayout gl = new GroupLayout(pane);
        pane.setLayout(gl);

        gl.setAutoCreateContainerGaps(true);
        gl.setHorizontalGroup(gl.createSequentialGroup().addComponent(arg[0]));
        gl.setVerticalGroup(gl.createParallelGroup().addComponent(arg[0]));
        pack();
    }

    private ImageIcon loadImage() {
        String file = getClass().getClassLoader().getResource("images/icon.png").getFile();
        ImageIcon icon = new ImageIcon(file);
        return icon;
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(()->{
            DisplayImage di = new DisplayImage();
            di.setVisible(true);
        });
    }
}
